<?LassoScript

//	Page Intercept
// 	Original concept provided by Pier Kuipers from the Algaebase project, included by his consent

//  FUNCTIONALITY
//	This routine determines the action to take when a page requires authorized access.
//  It will intercept the request, store it in the session, then upon successful login,
//  the login page will redirect to that page.
//  We store a session variable called $prelogin_request that contains the request.
//	When $svUser_ID and $svUserPrivs_Priv are blank, the current user has not logged in yet
//  so we set the "URL" variable to point at the login page.
//  Since $prelogin_request contains the URL they actually requested, the login process redirects to
//  that page (see login.lasso).
//  If $prelogin_request is blank, there's no redirect. This means they tried to login through the usual means.

// TO DO
// Could use a little cleanup

// Debugging
// Var('svDebug' = 'Y');
Debug;
	'<strong>page_intercept</strong><br>\n';
	('svUser_ID = '+(Var('svUser_ID'))+'<br>\n');
	('svUserPrivs_Priv = '+(Var('svUserPrivs_Priv'))+'<br>\n');
	('Response_Path = '+(Response_Path)+'<br>\n');
	('Response_FilePath = '+(Response_FilePath)+'<br>\n');
	('prelogin_request = '+(Var('prelogin_request'))+'<br>\n');
/Debug;

// Used to hold the redirect URL
Var('URL' = string);

// Add the prelogin request to the session
Session_AddVariable(-Name=$svSessionAdminName, 'prelogin_request');

	// Define $prelogin_request if not a login page (avoids a loop)
	// Don't do this for Heirarchy, as it loads in a new page and there is no navbar,
	// meaning the user can't get back. D'oh!
	If(!
		(Response_FilePath->contains('login')) ||
		(Response_FilePath->contains('logout')) ||
		(Response_FilePath->contains('manage_heirarchy')) 
	);

		If((Var('svUser_ID') == '') && (Var('svUserPrivs_Priv') == ''));
			If(!(Var_Defined('prelogin_request')));
				Var('prelogin_request' = Response_FilePath);
			/If;
		Else;
//			Var('prelogin_request' = Response_FilePath);
		/If;
		// Grab params from requested page
		If(Var('prelogin_request') == '');
			Var('prelogin_request' = Response_FilePath);
		/If;
		If(Client_GetParams->size>0);
			$prelogin_request += '?';
			Iterate((client_getparams), local('tmp'));
				$prelogin_request += ((#tmp->first)+'='+(#tmp->second)+'&');
			/Iterate;
			$prelogin_request->removetrailing('&');
		/If;
		Debug;
			('58: prelogin_request = '+($prelogin_request)+'<br>\n');
		/Debug;

		If(Response_FilePath->contains('login'));
//		If(($prelogin_request->contains('login')) || ($prelogin_request->contains('manage_heirarchy')));
			$URL = ('http://'+($svDomain)+('/login/'));
			Debug;
				('64 URL = '+($URL)+'<br>\n');
				('64 Response_FilePath = '+(Response_FilePath)+'<br>\n');
				('64 prelogin_request = '+(Var('prelogin_request'))+'<br>\n');
			/Debug;
			LI_URLRedirect(-Page=$URL,-UseError='N',-UseArgs='N');
		Else;
			$URL = $prelogin_request;
			Debug;
				('66 URL = '+($URL)+'<br>\n');
				('66 Response_FilePath = '+(Response_FilePath)+'<br>\n');
				('66 prelogin_request = '+(Var('prelogin_request'))+'<br>\n');
			/Debug;
			// Redirect ONLY if page is different to avoid an endless loop
			If(!($prelogin_request->BeginsWith($URL)));
				LI_URLRedirect(-Page=$URL,-UseError='N',-UseArgs='N');
			/If;
		/If;

	/If;

?>